package com.dementors.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import com.dementors.R;
import com.dementors.commonlibrary.xuye.base.BGAMoocStyleRefreshViewHolder;
import com.dementors.commonlibrary.xuye.base.BGARefreshLayout;
import com.dementors.commonlibrary.xuye.base.BaseFragment;
import com.dementors.commonlibrary.xuye.recycleview.DividerItemDecoration;
import com.dementors.data.API;
import com.dementors.model.FindItemBean;
import com.dementors.net.http.okhttp.callback.OkHttpCallback;
import com.dementors.net.http.okhttp.request.OkHttpRequest;
import com.dementors.ui.adapter.FindAdapter;
import com.dementors.utils.DisplayUtils;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import java.util.List;

/**
 * Created by Administrator on 2015/12/1 0001.
 */
public class FragmentLoops extends BaseFragment
    implements BGARefreshLayout.BGARefreshLayoutDelegate, View.OnClickListener {

  private FindAdapter mAdapter;
  private RecyclerView recyclerView;
  private BGARefreshLayout refreshLayout;
  private FloatingActionButton floatingActionButton;

  private void initHeaderView() {

    View headerView = View.inflate(getActivity(), R.layout.top_image_item, null);

    Picasso.with(getActivity())
        .load("http://b2.hucdn.com//upload/hmp/1507/01/28909217080316_222x132.png")
        .resize(DisplayUtils.getScreenW(getActivity()) / 3 - 20, 140)
        .into((ImageView) headerView.findViewById(R.id.image1_top));

    Picasso.with(getActivity())
        .load("http://b2.hucdn.com//upload/hmp/1508/14/35914613120316_333x198.png")
        .resize(DisplayUtils.getScreenW(getActivity()) / 3 - 20, 140)
        .into((ImageView) headerView.findViewById(R.id.image2_top));

    Picasso.with(getActivity())
        .load("http://b2.hucdn.com//upload/hmp/1508/10/78078448769962_222x132.jpg")
        .resize(DisplayUtils.getScreenW(getActivity()) / 3 - 20, 140)
        .into((ImageView) headerView.findViewById(R.id.image3_top));

    refreshLayout.setCustomHeaderView(headerView, true);
    refreshLayout.setRefreshViewHolder(
        new BGAMoocStyleRefreshViewHolder(getActivity(), true));
    //refreshLayout.setRefreshViewHolder(
    //    new BGANormalRefreshViewHolder(getActivity(), true));

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    //    // 为RecyclerView指定布局管理对象
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), 1));

    recyclerView.setAdapter(mAdapter);
  }

  @Override protected void initData() {
    new OkHttpRequest.Builder().url(API.FIND_URL).get(new OkHttpCallback<FindItemBean>() {
      @Override public void onError(Request request, Exception e) {

      }

      @Override public void onResponse(Response response, FindItemBean data) {
        List<FindItemBean.TimelinesEntity> timelines = data.getTimelines();
        mAdapter.update(timelines);
      }
    });
  }

  @Override protected void initView(Bundle savedInstanceState) {
    setContentView(R.layout.fragment_concern);
    refreshLayout = getViewById(R.id.refreshView_mine);
    recyclerView = getViewById(R.id.recyclerView_concern);
    floatingActionButton = getViewById(R.id.main_fab);
  }

  @Override protected void setListener() {

    refreshLayout.setDelegate(this);
    floatingActionButton.setOnClickListener(this);
    mAdapter = new FindAdapter(getActivity());
  }

  @Override protected void processLogic(Bundle savedInstanceState) {

    initHeaderView();
  }

  @Override protected void onUserVisible() {

  }

  @Override
  public void onBGARefreshLayoutBeginRefreshing(final BGARefreshLayout refreshLayout) {

    new Handler().postDelayed(new Runnable() {
      @Override public void run() {
        refreshLayout.endRefreshing();
      }
    }, 5000);
  }

  @Override public boolean onBGARefreshLayoutBeginLoadingMore(
      final BGARefreshLayout refreshLayout) {
    new Handler().postDelayed(new Runnable() {
      @Override public void run() {
      }
    }, 5000);
    return true;
  }

  @Override public void onClick(View v) {
    recyclerView.smoothScrollToPosition(0);

  }
}
